#include "mytcpsocket.h"
#include <QDebug>

MyTcpSocket::MyTcpSocket()
{
    connect(this,SIGNAL(readyRead())
            ,this,SLOT(recvMsg()));
    connect(this,SIGNAL(disconnected())
            ,this,SLOT(clientOffline()));
}

QString MyTcpSocket::getName()
{
    return m_strName;
}

void MyTcpSocket::recvMsg()
{
    qDebug()<<this->bytesAvailable();
    uint uiPDULen = 0;
    this->read((char*)&uiPDULen,sizeof(uint));
    uint uiMsgLen = uiPDULen-sizeof(PDU);
    PDU *pdu = mkPdu(uiMsgLen);
    this->read((char*)pdu+sizeof(uint),uiPDULen-sizeof(uint));
    switch (pdu->uiMsgType) {
    case ENUM_MSG_TYPE_REGIST_REQUEST:{
        char caName[32]= {'\0'};
        char caPwd[32] = {'\0'};
        strncpy(caName,pdu->caData,32);
        strncpy(caPwd,pdu->caData+32,32);
        qDebug()<<caName<<caPwd<<pdu->uiMsgType;
        bool ret = OpeDB::getinstance().handleRegist(caName,caPwd);
        PDU *respdu = mkPdu(0);
        respdu->uiMsgType = ENUM_MSG_TYPE_REGIST_RESPOND;
        if(ret){
            strcpy(respdu->caData,REGIST_OK);
        }else{
            strcpy(respdu->caData,REGIST_FAILED);
        }
        write((char*)respdu,respdu->uiPDULen);
        free(respdu);
        respdu=NULL;
        break;
    }
    case ENUM_MSG_TYPE_LOGIN_REQUEST:{
        char caName[32]= {'\0'};
        char caPwd[32] = {'\0'};
        strncpy(caName,pdu->caData,32);
        strncpy(caPwd,pdu->caData+32,32);
        bool ret = OpeDB::getinstance().handleLogin(caName,caPwd);
        PDU *respdu = mkPdu(0);
        respdu->uiMsgType = ENUM_MSG_TYPE_LOGIN_RESPOND;
        if(ret){
            strcpy(respdu->caData,LOGIN_OK);
            m_strName = caName;
        }else{
            strcpy(respdu->caData,LOGIN_FAILED);
        }
        write((char*)respdu,respdu->uiPDULen);
        free(respdu);
        respdu=NULL;
        break;
    }
    case ENUM_MSG_TYPE_ALLONLINE_REQUEST:{
        QStringList ret = OpeDB::getinstance().handleAllOnline();
        uint uiMsgLen = ret.size()*32;
        PDU *respdu =mkPdu(uiMsgLen);
        respdu->uiMsgType =ENUM_MSG_TYPE_ALLONLINE_RESPOND;
        for(int i=0;i<ret.size();i++){
            memcpy((char*)(respdu->caMsg+i*32)
                   ,ret.at(i).toStdString().c_str()
                   ,ret.at(i).size());
        }
        write((char*)respdu,respdu->uiPDULen);
        delete(respdu);
        respdu = NULL;
        break;
    }
    case ENUM_MSG_TYPE_SEARCH_USR_REQUEST:{
        int ret = OpeDB::getinstance().handleSearchUsr(pdu->caData);
        PDU *respdu =mkPdu(0);
        respdu->uiMsgType = ENUM_MSG_TYPE_SEARCH_USR_RESPOND;
        if(-1==ret){
            strcpy(respdu->caData,SEARCH_USR_NO);

        }else if(1==ret){
            strcpy(respdu->caData,SEARCH_USR_ONLINE);

        }else if(0==ret){
            strcpy(respdu->caData,SEARCH_USR_OFFONLINE);
        }
        write((char*)respdu,respdu->uiPDULen);
        delete(respdu);
        respdu = NULL;
        break;
    }
    default:
        break;
    }
    delete(pdu);
    pdu=NULL;
    //    qDebug()<<caName<<caPwd<<pdu->uiMsgType;
}

void MyTcpSocket::clientOffline()
{
    OpeDB::getinstance().handleOffline(m_strName.toStdString().c_str());
    emit offline(this);
}
